import React, {Component} from 'react';
import './index.less';
import {base} from 'nc-lightapp-front';
import setButtonStatus from "../../container/functions/setButtonStatus";
import Pagination from "../../../../../../hrpub/common/components/Pagination";
import checkPsnInfo from "../../container/functions/checkPsnInfo";

class MainTable extends Component {
    constructor(props) {
        super(props);
        this.state = {tableHeight: 0};
        this.changePage = this.changePage.bind(this);
        this.pageSizeChange = this.pageSizeChange.bind(this);
        this.checkDetail = this.checkDetail.bind(this);
    }

    componentDidMount() {
        const height = document.querySelector('.entry-staff-query-content').clientHeight;
        this.setState({tableHeight: height - 78});
    }

    changePage(pageIndex) {
        const {
            updateState,
            getMainTableData,
            pageInfo
        } = this.props;

        updateState({
            pageInfo: {
                ...pageInfo,
                pageIndex: pageIndex
            }
        }, () => {
            getMainTableData();
        });
    }

    pageSizeChange(pageSize) {
        const {
            updateState,
            getMainTableData,
            pageInfo
        } = this.props;

        updateState({
            pageInfo: {
                ...pageInfo,
                pageSize,
                pageIndex: 1
            }
        }, () => {
            getMainTableData();
        });
    }

    checkDetail(record, index, props, e) {
        const {updateState, initCardForm, orgVal, includeCheckedPsn} = this.props;
        checkPsnInfo.call(this, {
            orgVal,
            includeCheckedPsn,
            pk_psndoc_bad: record.values.pk_psndoc_bad.value
        }, () => {
            updateState({
                page: 'card',
                pk_psndoc_bad: record.values.pk_psndoc_bad.value
            }, () => {
                setButtonStatus.call(this, 'card');
                initCardForm();
            })
        })
    }

    render() {
        const {
            editTable,
            pageInfo
        } = this.props;

        return (
            <div className='main-table'>
                {editTable.createEditTable('blacklist', {
                    height: this.state.tableHeight,
                    showCheck: false,
                    showIndex: true,
                    adaptionHeight: true,
                    onRowDoubleClick: this.checkDetail
                })}
                {
                    pageInfo.total > 0 &&
                    <Pagination
                        current={parseInt(pageInfo.pageIndex)}
                        pageSize={parseInt(pageInfo.pageSize)}
                        pageSizeOptions={[5, 10, 20, 50, 100]}
                        showSizeChanger={true}
                        total={pageInfo.total}
                        onShowSizeChange={this.pageSizeChange}
                        onChange={this.changePage}
                    />
                }
                {/*<Pagination
                    {...this.props}
                    pageInfo={pageInfo}
                    onPageChange={this.changePage}
                    onPageSizeChange={this.pageSizeChange}/>*/}
            </div>
        );
    }
}

export default MainTable;
